import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateStarDto } from './dto/create-star.dto';
import { InjectEntityManager } from '@nestjs/typeorm';
import { EntityManager } from 'typeorm';
import { Star } from './entities/star.entity';
import { _BusinessTypeList } from '../utils';
import { Blog } from '../blog/entities/blog.entity';
// import { Daily } from '../daily/entities/daily.entity';

@Injectable()
export class StarService {
  @InjectEntityManager()
  private manage: EntityManager

  // 收藏
  async create(createLikeDto: CreateStarDto) {
    const {createId, userId, authorId, type} = createLikeDto
    const star = new Star();
    star.createId = createId;
    star.userId = userId;
    star.authorId = authorId;
    star.type = type;
    star[_BusinessTypeList[type] + 'Id'] = createId
    try {
      const res = await this.manage.save(Star, star);
      if (res?.id) {
        let incrementResult = null
        if (type == 1) {
          incrementResult = await this.manage.increment(
            Blog,
            { id: createId },
            'star_times',
            1,
          );
        } 
        // else if (type == 2) {
        //   incrementResult = await this.manage.increment(
        //     Daily,
        //     { id: createId },
        //     'star',
        //     1,
        //   );
        // } else if (type == 3) {
        //   incrementResult = await this.manage.increment(
        //     Comment,
        //     { id: createId },
        //     'star',
        //     1,
        //   );
        // }
        if (incrementResult?.affected === 1) {
          return '点赞成功';
        } else {
          throw new HttpException('点赞失败', HttpStatus.BAD_REQUEST);
        }
      } else {
        throw new HttpException('点赞失败', HttpStatus.BAD_REQUEST);
      }
    } catch (error) {
      throw new HttpException(error, HttpStatus.BAD_REQUEST);
    }
  }

  // 取消收藏
  async remove(data: {
    id?: number;
    type: number;
    createId: number;
    userId?: number;
  }) {
    const { id, type, createId, userId } = data;
    if (id) {
      await this.manage.delete(Star, id);
    } else {
      await this.manage.delete(Star, {
        createId,
        type,
        userId,
      });
    }
    if (type == 1) {
      return await this.manage.decrement(
        Blog,
        { id: createId },
        'star_times',
        1,
      );
    } 
    // else if (type == 2) {
    //   return await this.manage.decrement(Daily, { id: createId }, 'like', 1);
    // } else if (type == 3) {
    //   return await this.manage.decrement(Comment, { id: createId }, 'like', 1);
    // }
  }


  // 业务id + 业务类型查询
  async findByCreateIdAndType (data: {createId: number, type: number, userId?: number}) {
    const where: {createId?: number, type?: number, userId?: number} = {userId: data.userId}
    if(data?.createId) {
      where.createId = data.createId
    }

    if(data?.type) {
      where.type = data.type
    }
    if(data?.createId) {
      where.createId = data.createId
    }

    if(data?.userId) {
      where.userId = data.userId
    }
    const [starList, total] = await this.manage.findAndCount(Star, {
      where: where
    })
    return {
      data: starList,
      total
    }
  }

  async queryStarTotal(userId: number) {
    try {
      const [, total] = await this.manage.findAndCountBy(Star, {userId: userId})
      return {
        total
      }
    } catch (error) {
      throw new HttpException(error, HttpStatus.BAD_REQUEST)
    }
  }
}
